#!/bin/bash

echo "------------------------------------------------------------------------------------------------------------------------------------------------------"
echo -e "\033[33m LVM DISK Auto Partition Tools Version: 5.2.0 (20220706) \033[0m |  Technical support provided by [ https://www.cxthhhhh.com ] "
echo "------------------------------------------------------------------------------------------------------------------------------------------------------"
echo -e "\033[33m 8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 \033[0m"
echo -e "\033[33m 8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 \033[0m"
echo -e "\033[33m 88888888888888888888888   8888888    888888    88      8888888     88888888           88888   8888         888    888    888888888888888888888888888 \033[0m"
echo -e "\033[33m 88888888888888888888888   88888888   888888   888      888888      88888888   88888     888   888    888888888    88   88888888888888888888888888888 \033[0m"
echo -e "\033[33m 88888888888888888888888   888888888   8888   8888   8   8888   8   88888888   8888888   888   888     88888888    8   888888888888888888888888888888 \033[0m"
echo -e "\033[33m 88888888888888888888888   888888888    88   88888   88   88   88   88888888   8888888   888   88888       8888       8888888888888888888888888888888 \033[0m"
echo -e "\033[33m 88888888888888888888888   8888888888   88   88888   888   8  888   88888888   888888    888   888888888     88    8    88888888888888888888888888888 \033[0m"
echo -e "\033[33m 88888888888888888888888   88888888888      888888   888      888   88888888   88888    8888   8888888888    88    88    8888888888888888888888888888 \033[0m"
echo -e "\033[33m 88888888888888888888888         888888    8888888   8888    8888   88888888          888888   888          888    8888    88888888888888888888888888 \033[0m"
echo -e "\033[33m 8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 \033[0m"
echo -e "\033[33m 8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 \033[0m"
echo -e "\033[33m 88888888   888888888888888888888888888888888888888888888888       88888888888888888888888888888888888    88888888888   88888888888888888888888888888 \033[0m"
echo -e "\033[33m 8888888     888888888888888888888   88888888888888888888888          888888888888888888888888   88888   8888   88888   88888888888888888888888888888 \033[0m"
echo -e "\033[33m 888888   8   88888888888888888888   88888888888888888888888   8888    88888888888888888888888   888888888888   8888888888888888888888888888888888888 \033[0m"
echo -e "\033[33m 88888   88   88888   88888   88       888          88888888   8888    88        8888       8       88   88        88   8888         8888          88 \033[0m"
echo -e "\033[33m 8888   8888   8888   88888   8888   8888   88888    8888888          888888888   888    88888   88888   8888   88888   888   88888   888   8888   88 \033[0m"
echo -e "\033[33m 888            888   88888   8888   8888   88888    8888888   8888888888         888   888888   88888   8888   88888   88    88888   888   8888   88 \033[0m"
echo -e "\033[33m 888   888888    88    888    8888   8888   88888   88888888   888888888   8888   888   888888    8888   8888   88888   888   88888   888   8888   88 \033[0m"
echo -e "\033[33m 88    8888888   888          8888     888         888888888   888888888          888   8888888     88   88888     88   8888         8888   8888   88 \033[0m"
echo -e "\033[33m 8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 \033[0m"
echo "------------------------------------------------------------------------------------------------------------------------------------------------------"
echo -e "\033[33m LVM DISK Auto Partition Tools Version: 5.2.0 (20220706) \033[0m |  Technical support provided by [ https://www.cxthhhhh.com ] "
echo -e " [ Supported by https://github.com/MeowLove ]  |  [ Supported by https://www.caoxiaotian.com ] "
echo "------------------------------------------------------------------------------------------------------------------------------------------------------"
echo "Analyze Disk type (Virtual/Physical)......"
sleep 3

# 获取磁盘，为保证数据安全，以下仅对第一个磁盘，通常为数据盘。进行操作。
# (如需修改其他磁盘，请手动修改本分区程序)
DISK=`ls /dev/*da | head -1`
DISK2=`ls /dev/*db | head -1`
DISK3=`ls /dev/*dc | head -1`

# 设置新的自动拓展分区ID（默认值）
PART=$DISK"0"

# 获取VG
VGNAME=`lvdisplay | grep "VG Name" | awk ' ''{print $3}'`
# 获取LV
LVNAME=`lvdisplay | grep "LV Name" | awk ' ''{print $3}'`

# 判断MBR还是GPT
CXTDTYPE=`fdisk -l | grep -o gpt | head -1`
if [ $CXTDTYPE == "gpt" ] || [ $CXTDTYPE == "GPT" ];then
# 创新新的GPT分区为LVM
echo "(GPT) Creating new partition..."
PART=$DISK"4"
echo "n
4


t
4
lvm
w
" | fdisk $DISK
# 新的GPT分区为LVM创建完毕
else
# 创新新的MBR分区为LVM
echo "(MBR) Creating new partition..."
PART=$DISK"3"
echo "n
p
3


t
3
8e
w
" | fdisk $DISK
# 新的MBR分区为LVM创建完毕
fi

# 等待磁盘响应
sleep 10s

# 同步磁盘
echo "Syncing disk..."
partprobe
sleep 20s

# 创建PV
echo "Creating PV..."
pvcreate $PART
sleep 10s

# 拓展VG
echo "Extending VG..."
vgextend $VGNAME $PART
sleep 10s

# 拓展LV
echo "Extending LV..."
lvextend -l +100%FREE /dev/mapper/$VGNAME-$LVNAME
sleep 10s

# 刷新卷组大小
echo "Resizing volume..."
resize2fs -p /dev/mapper/$VGNAME-$LVNAME
sleep 6s

echo "Done! Please restart your server."
